package strategy;

import cn.hutool.core.thread.ThreadFactoryBuilder;
import cn.hutool.core.thread.ThreadUtil;

import java.util.Arrays;
import java.util.concurrent.*;

/**
 * @ClassName : main
 * @Description : 主方法
 * @Author : gyh
 * @Date: 2021-09-14 00:29
 */
public class main {
    public static void main(String[] args) throws Exception {
        ThreadFactory namedThreadFactory = new ThreadFactoryBuilder().setNamePrefix("thread-").build();
        ThreadPoolExecutor executor = new ThreadPoolExecutor(5, 15, 3, TimeUnit.SECONDS, new LinkedBlockingDeque<>(100), namedThreadFactory);

        for (int i = 0; i < 1000; i++) {
            MyTask myTask = new MyTask(i);
            executor.execute(myTask);
            System.out.println("线程池中线程数目：" + executor.getPoolSize() + "，队列中等待执行的任务数目：" +
                    executor.getQueue().size() + "，已执行玩别的任务数目：" + executor.getCompletedTaskCount());
        }
        executor.shutdown();
    }
}

class MyTask implements Runnable {
    private int taskNum;

    public MyTask(int num) {
        this.taskNum = num;
    }

    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName() + "正在执行task " + taskNum);
        try {
            Thread.currentThread().sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("task " + taskNum + "执行完毕");
    }
}
